<?php

use PhpParser\Error;
use PhpParser\ParserFactory;
use PhpParser\PrettyPrinter;

function preg_split_end(string $pattern, string $subject)
{
	$pattern = $pattern;
	preg_match_all($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);

	if (!empty($matches[0])) {
		$lastMatch = end($matches[0]); // 获取最后一个匹配项
		$endPos = $lastMatch[1] + strlen($lastMatch[0]); // 计算结束位置
		$result = substr($subject, 0, $endPos); // 截取到结束位置
	} else {
		$result = $subject; // 无匹配时返回原字符串
	}
	return $result;
}

// 优化解密内容
function youhua($hanshu, $cont, $count = 150)
{
	$jiance =  preg_match('/' . $hanshu . '/ims', $cont);
	if (empty($jiance)) {
		return $cont;
	}
	$jiance = preg_match_all('/\$.{6}=\'.{0,' . $count . '}\';' . $hanshu . ' \$.{6};/ims', $cont, $matches);
	if (empty($jiance)) {
		return $cont;
	}
	// 用于搜索的表达式
	$pattern = array();
	// 用于替换的表达式
	$strs = array();
	for ($i = 0; $i < count($matches[0]); $i++) {
		$str = $matches[0][$i];
		$quote = preg_quote($str, '/');
		$pattern[] = '/' . $quote . '/ims';
		preg_match('/\$.{6}=(\'.{0,' . $count . '}\';)' . $hanshu . ' \$.{6};/ims', $str, $matchs);
		$neirong = $hanshu . ' ' . $matchs[1];
		$strs[] = $neirong;
	}
	$content = preg_filter($pattern, $strs, $cont);
	return $content;
}
function hanshu($data)
{
	$jiance = preg_match_all('/call_user_func_array\("\w+",array\(.{0,40}\)\)/ims', $data, $matches);
	if (empty($jiance)) {
		return $data;
	}
	// 用于搜索的表达式
	$pattern = array();
	// 用于替换的表达式
	$strs = array();
	for ($i = 0; $i < count($matches[0]); $i++) {
		$str = $matches[0][$i];
		$quote = preg_quote($str, '/');
		$pattern[] = '/' . $quote . '/ims';
		preg_match('/call_user_func_array\("(\w+)",array(\(.{0,40}\))\)/ims', $str, $matchs);
		$strs[] = preg_replace('/&/ims', '', $matchs[1] . $matchs[2]);
		// $strs[] = $matchs[1].$matchs[2];
	}
	$content = preg_filter($pattern, $strs, $data);
	return $content;
}
function bin_h($data)
{
	$content = preg_replace('/\$\w+=\&?\$GLOBALS\[(\w+)\]\[00?\];/ims', '', $data);
	return $content;


	// 以下内容作废
	$jiance = preg_match_all('/\$\w+=\&?\$GLOBALS\[(\w+)\]\[0\];/ims', $data, $matches);
	if (empty($jiance)) {
		return $data;
	}
	// 用于搜索的表达式
	$pattern = array();
	// 要替换的内容
	$strs = array();
	for ($i = 0; $i < count($matches[0]); $i++) {
		$str = $matches[1][$i];
		$quote = preg_quote($matches[0][$i], '/');
		$pattern[] = '/' . $quote . '/ims';
		$num = rand() . '.php';
		$str = '<?php $string = $GLOBALS[' . $str . '][0]?>';
		wj_w($num, $str);
		include($num);
		unlink($num);
		$strs[] = "'" . $string . "'";
	}
	// return $pattern;
	$content = preg_filter($pattern, $strs, $data);
	return $content;
}
function array_is($data)
{
	$content = preg_replace('/if\(is_array\(\$\w+\[\w+\]\)\)goto \w{0,15};goto \w{0,15};\w{0,15}:goto \w{0,10};\w{0,10}:\w{0,10}:/ims', '', $data);
	$content =  preg_replace('/if\(is_array\(\$\w+\[\w+\]\)\)goto \w{0,20};goto \w{0,20};\w{0,20}:/ims', '', $content);
	return $content;
}
function md_5($data)
{
	$content = $data;
	$jiance = preg_match_all('/(&?\$GLOBALS\[\w+\]\[\w+\])/ims', $content, $matches);
	if (empty($jiance)) {
		return $data;
	}
	// 要被替换的正则
	$pattern = array();
	// 要替换的内容
	$strs = array();
	for ($i = 0; $i < count($matches[0]); $i++) {
		$str = $matches[0][$i];
		$quote = preg_quote($matches[1][$i], '/');
		$pattern[] = '/' . $quote . '/ims';
		$num = rand() . '.php';
		$str = '<?php $string = ' . $str . '?>';
		wj_w($num, $str);
		include($num);
		unlink($num);
		if (!function_exists($string)) {
			$string = "'" . $string . "'";
		}
		$strs[] = $string;
	}
	$content = preg_filter($pattern, $strs, $data);
	$content = preg_replace("/''H\*''/ims", "'H*'", $content);
	$content = preg_replace('/\$\w+=\'H\*\';/ims', '', $content);
	return $content;
}
function packs($data)
{
	$content = $data;
	$jiance = preg_match_all('/pack\(\'H\*\',\'\w+\'\)/ims', $content, $matches);
	// return $matches;
	// 用于搜索的表达式
	$pattern = array();
	// 要替换的内容
	$strs = array();
	for ($i = 0; $i < count($matches[0]); $i++) {
		$str = $matches[0][$i];
		$quote = preg_quote($str, '/');
		$pattern[] = '/' . $quote . '/ims';
		$num = rand() . '.php';
		$str = '<?php $string = ' . $str . '?>';
		wj_w($num, $str);
		include($num);
		unlink($num);
		// if (!function_exists($string)) {
		$string = "'" . $string . "'";
		// }
		$strs[] = $string;
	}
	// return $strs;
	$content = preg_filter($pattern, $strs, $data);
	return $content;
}
function echo_conf($cont)
{
	// echo ?\$conf\[\$\w+\]
	$jiance = preg_match_all('/\$\w+=\'\w+\';echo ?\$conf\[\$\w+\]/ims', $cont, $matches);
	if (empty($jiance)) {
		return $cont;
	}
	// 用于搜索的表达式
	$pattern = array();
	// 用于替换的表达式
	$strs = array();
	for ($i = 0; $i < count($matches[0]); $i++) {
		$str = $matches[0][$i];
		$quote = preg_quote($str, '/');
		$pattern[] = '/' . $quote . '/ims';
		preg_match('/\$\w+=(\'\w+\');echo ?\$conf\[\$\w+\]/ims', $str, $matchs);
		$neirong = 'echo $conf[' . $matchs[1] . ']';
		$strs[] = $neirong;
	}
	// return $pattern;
	$content = preg_filter($pattern, $strs, $cont);
	return $content;
}
function goto_if($data)
{
	$content = $data;
	$content = preg_match_all('/if\(\$\w+\)(goto (\w+);)/ims', $content, $matches);
	// 用于搜索的表达式
	$pattern = array();
	for ($i = 0; $i < count($matches[0]); $i++) {
		$str = $matches[1][$i];
		$quote = preg_quote($str, '/');
		$pattern[] = '/' . $quote . '/ims';
	}
	// $content = preg_filter($pattern, '', $data);
	// 用于搜索的表达式
	$pattern2 = array();
	for ($i = 0; $i < count($matches[0]); $i++) {
		$str = $matches[3][$i] . ':';
		$quote = preg_quote($str, '/');
		// $pattern2[] = '/' . $quote . '/ims';
		$pattern2[] = $quote;
	}
	// for ($i = 0; $i < count($matches[0]); $i++) {
	//     preg_match('/' . $pattern2[$i] . '.+/ims');
	// }
}
function go_to1($data)
{
	$content = $data;
	$content = preg_match_all('/echo \'.{0,100}\';(goto \w+;\w+:\w+:goto \w+;\w+:\w+:)echo \'.{0,100}\'[$;]/ims', $content, $matches);
	// return $matches;
	// 用于搜索的表达式
	$pattern = array();
	for ($i = 0; $i < ((count($matches[0]) - 0)); $i++) {
		$str = $matches[1][$i];
		$quote = preg_quote($str, '/');
		$pattern[] = '/' . $quote . '/ims';
	}
	// return $pattern;
	$content = preg_replace($pattern, '', $data);
	return $content;
}
function go_to2($data)
{
	$content = $data;
	$content = preg_match_all('/echo \'.{0,100}\';(goto \w+;\w+:\w+:goto \w+;\w+:\w+:)echo \$conf\[\'.{0,100}\'\][$;]/ims', $content, $matches);
	// return $matches;
	// 用于搜索的表达式
	$pattern = array();
	for ($i = 0; $i < ((count($matches[0]) - 0)); $i++) {
		$str = $matches[1][$i];
		$quote = preg_quote($str, '/');
		$pattern[] = '/' . $quote . '/ims';
	}
	// return $pattern;
	$content = preg_replace($pattern, '', $data);
	return $content;
}
function go_to3($data)
{
	$content = $data;
	$content = preg_match_all('/echo \$conf\[\'.{0,100}\'\][$;](goto \w+;\w+:\w+:goto \w+;\w+:\w+:)echo \'.{0,100}\';/ims', $content, $matches);
	// return $matches;
	// 用于搜索的表达式
	$pattern = array();
	for ($i = 0; $i < ((count($matches[0]) - 0)); $i++) {
		$str = $matches[1][$i];
		$quote = preg_quote($str, '/');
		$pattern[] = '/' . $quote . '/ims';
	}
	// return $pattern;
	$content = preg_replace($pattern, '', $data);
	return $content;
}
function go_to4($data)
{
	$content = $data;
	$content = preg_match_all('/echo \'.{0,100}\';(goto \w+;\w+:\w+:)echo \'.{0,100}\'[$;](goto \w+;\w+:\w+:)/ims', $content, $matches);
	// return $matches;
	// 用于搜索的表达式
	$pattern = array();
	$pattern2 = array();
	for ($i = 0; $i < ((count($matches[0]) - 0)); $i++) {
		$str = $matches[1][$i];
		$quote = preg_quote($str, '/');
		$pattern[] = '/' . $quote . '/ims';

		$str2 = $matches[2][$i];
		$quote2 = preg_quote($str2, '/');
		$pattern2[] = '/' . $quote2 . '/ims';
	}
	// return $pattern;
	$content = preg_replace($pattern, '', $data);
	$content = preg_replace($pattern2, 'null', $content);
	return $content;
}
function wj_w($name, $txt)
{
	$fopen = fopen($name, 'w');
	$fwrite = fwrite($fopen, $txt);
	fclose($fopen);
	if ($fwrite) {
		return true;
	}
}
function zy($data)
{
	$data = trim($data);
	if (preg_match("/'/ims", $data)) {
		return preg_replace("/'/ims", "\'", $data);
	} else {
		return $data;
	}
}
function format_code($content)
{
	$data = $content;
	$autoload_file = dirname(__DIR__) . 'system/vendor/autoload.php';
	if (file_exists($autoload_file)) {
		require dirname(__DIR__) . 'system/vendor/autoload.php';
	} else {
		return null;
	}
	$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
	try {
		$ast = $parser->parse($content);
	} catch (Error $error) {
		echo "Parse error: {$error->getMessage()}\n";
		return;
	}
	$prettyPrinter = new PrettyPrinter\Standard;
	$content = $prettyPrinter->prettyPrintFile($ast);
	if (empty($content)) {
		return false;
	}
	return $content;
}
